<script setup>
import {reactive} from "vue";
import {requestLogin} from "@/api";

const beforeClose = (action) =>
    new Promise((resolve) => {
      if (action === 'confirm') {
        if (loginGroup.login){
          storageGroup.saveUserInfo();
        }
        dialogGroup.show = false;
        resolve(true);
      } else {
        // 拦截取消操作
        resolve(false);
      }
    })

const dialogGroup = reactive({
  title: "登录",
  message: "默认",
  show: false,
})

const storageGroup = reactive({
  saveUserInfo(){
    uni.setStorageSync("userInfo",{
      login_state:loginGroup.login,
      account:loginGroup.account
    })
    uni.redirectTo({url:"../user/user"});
  }
})

const routerGroup = reactive({
  linkRegister(){
    uni.redirectTo({url:"../register/register"})
  }
})

const loginGroup = reactive({
  account:"",
  password:"",
  login:false,
  async userLogin(){
    let result = await requestLogin({account:this.account, password:this.password})
    if (result.statusCode === 200){
      if (result.data === "DataFormatError"){
        dialogGroup.message = "账号、密码格式错误";
      }else if (result.data === "EmptyError"){
        dialogGroup.message = "账号、密码为空";
      }else if (result.data === "Normal"){
        this.login = true;
        dialogGroup.message = "登录成功";
      }else if (result.data === "DatabaseError"){
        dialogGroup.message = "数据库错误";
      }else if (result.data === "DataNotFoundError"){
        dialogGroup.message = "账号不存在，请验证";
      }else if (result.data === "DataError"){
        dialogGroup.message = "账号或者密码错误";
      }else{
        dialogGroup.message = "服务器错误，请联系管理员";
      }
    }
    dialogGroup.show = true;
  }
})
</script>

<template>
  <van-dialog
      :before-close="beforeClose"
      :message="dialogGroup.message"
      :show="dialogGroup.show"
      :title="dialogGroup.title"
      custom-class="my-custom-class"
  />
  <view class="login-page">
    <scroll-view scroll-y="true">
      <view class="login-box">
        <view class="login">
          <view class="login-title-box">
            <view class="login-title">
              <text class="login-title-text">登录</text>
            </view>
          </view>
          <view class="login-input-box">
            <view class="login-input">
              <view class="login-input-block">
                <text class="login-input-text">账号:</text>
                <input class="login-input-account" type="text" placeholder="请输入您的账号" v-model="loginGroup.account">
              </view>
              <br />
              <view class="login-input-block">
                <text class="login-input-text">密码:</text>
                <input class="login-input-password" type="password" placeholder="请输入你的密码" v-model="loginGroup.password"/>
              </view>
            </view>
          </view>
          <view class="login-btn-box">
            <van-button class="login-btn" type="default" @click="loginGroup.userLogin()">登录</van-button>
            <van-button class="login-btn" type="default" @click="routerGroup.linkRegister()">注册</van-button>
          </view>
        </view>
      </view>
    </scroll-view>
  </view>
</template>

<style scoped src="../../css/login.css">

</style>